package module

import (
    "context"
    "fmt"
    "net/http"
    "time"

    "gitee.com/shijingzhao/z-frame/config"
    "gitee.com/shijingzhao/z-frame/module/pb"
    "gitee.com/shijingzhao/z-frame/tools"
    "gitee.com/shijingzhao/z-frame/zhttp"
    "gitee.com/shijingzhao/z-frame/zlog"
    "gitee.com/shijingzhao/z-frame/zrpc"
    "gitee.com/shijingzhao/z-frame/zscript"
)

type RS struct {
    Name      string      `json:"name"`
    UserDB    string      `json:"user_db"`
    UserRedis string      `json:"user_redis"`
    From      string      `json:"from"`
    Time      string      `json:"time"`
    R         interface{} `json:"r"`
}

type Params struct {
    From string `json:"from"`
    Name string `json:"name"`
}

func Hello1Handler(w http.ResponseWriter, r *http.Request) {
    defer tools.FunExecTime("function.Hello1Handler")()

    l := zlog.NewHttpLogger(context.TODO(), "hello1", r)

    var params Params
    zhttp.ParamsAnalysis(r, &params)

    fmt.Println("r.Form", r.Form)

    rs := &RS{
        Name:      "hello1",
        UserDB:    config.DBConfig.UserDB,
        UserRedis: config.DBConfig.UserRedis,
        From:      params.From,
        Time:      time.Now().Format("2006-01-02 15:04:05.000000"),
        R:         params,
    }

    defer func() {
        l.Info("rs", zlog.Struct("rs", rs), zlog.Struct("rq", params))
    }()

    zhttp.OutputJson(w, 200, "success", rs)
}

func Hello1HandlerMiddleware(next http.HandlerFunc) http.HandlerFunc {
    return func(writer http.ResponseWriter, request *http.Request) {
        zlog.NewHttpLogger(context.TODO(), "hello1", request).Info("Hello1HandlerMiddleware")
        next(writer, request)
    }
}

func Hello2HandlerMiddleware(next http.HandlerFunc) http.HandlerFunc {
    return func(writer http.ResponseWriter, request *http.Request) {
        zlog.NewHttpLogger(context.TODO(), "hello1", request).Info("Hello2HandlerMiddleware")
        next(writer, request)
    }
}

func Hello1HandlerScript(ctx context.Context, name string, params map[string]interface{}) {
    line := 0
    for {
        if zscript.StopSignal(ctx) {
            zlog.NewSystemLogger(context.TODO()).System(
                "script服务器终止",
                zlog.String("name", name),
                zlog.Interface("params", params),
                zlog.Int("line", line),
            )
            return
        }

        zlog.NewScriptLogger(context.TODO(), name).System(
            "script进行",
            zlog.String("name", name),
            zlog.Interface("params", params),
            zlog.Int("line", line),
        )

        line += 1

        hrq := &HelloRQ{}
        hrs := &HelloRS{}
        if err := zrpc.NewClient(ctx, config.DBConfig.HelloRpcConfig).Call(hrq, hrs); err != nil {
            fmt.Println("Hello.World.Err", err.Error())
        }
        fmt.Println("Hello.World", hrs.S)

        arq := &pb.ArithRequest{
            A: 4,
            B: 2,
        }
        ars := &pb.ArithResponse{}
        if err := zrpc.NewClient(ctx, config.DBConfig.ArithRpcConfig).Call(arq, ars); err != nil {
            fmt.Println("Arith.Multiply.Err", err.Error())
        }
        fmt.Println("Arith.Multiply", ars.C)

        time.Sleep(1 * time.Second)
    }
}

type HelloRQ struct {
    Q string
}

type HelloRS struct {
    S string
}

type Airth struct{}

func (a *Airth) ArithAddHandlerRpc(rq *pb.ArithRequest, rs *pb.ArithResponse) error {
    rs.C = rq.A + rq.B
    return nil
}

func (a *Airth) ArithSubHandlerRpc(rq *pb.ArithRequest, rs *pb.ArithResponse) error {
    rs.C = rq.A - rq.B
    return nil
}
